package simulatorGUI;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class Combinations extends javax.swing.JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JScrollPane jScrollPane1;
	private JLabel lblHeader;
	private JLabel lblX;
	private JButton btnCompute;
	private JLabel lblCell;
	private JTextField txtId;
	private JLabel lblY;
	private JTable jTable1;
	private String[][] probs;

	public Combinations(int rowSize, int[] icyPatches) {
		super();
		initGUI(rowSize, icyPatches);
	}

	private void initGUI(final int rowSize, int[] icyPatches) {
		try {
			String[] header = new String[icyPatches.length+1];
			probs = new String[icyPatches.length][icyPatches.length];
			header[0] = "";
			for (int i=0; i<icyPatches.length; i++){
				header[i+1] = icyPatches[i]+"";
				probs[i][0] = icyPatches[i]+"";
			}
			setPreferredSize(new Dimension(400, 300));
			this.setLayout(null);
			{
				jScrollPane1 = new JScrollPane();
				this.add(jScrollPane1);
				jScrollPane1.setBounds(23, 41, 356, 248);
				{
					TableModel jTable1Model = new DefaultTableModel(probs, header);
					jTable1 = new JTable();
					jScrollPane1.setViewportView(jTable1);
					jTable1.setModel(jTable1Model);
					
				}
			}
			{
				lblHeader = new JLabel();
				this.add(lblHeader);
				lblHeader.setText("Joint probability");
				lblHeader.setBounds(18, 11, 108, 14);
				lblHeader.setFont(new java.awt.Font("Tahoma",1,12));
			}
			{
				lblX = new JLabel();
				this.add(lblX);
				lblX.setText("X:");
				lblX.setBounds(335, 4, 44, 14);
			}
			{
				lblY = new JLabel();
				this.add(lblY);
				lblY.setText("Y:");
				lblY.setBounds(335, 23, 44, 14);
			}
			{
				txtId = new JTextField();
				this.add(txtId);
				txtId.setBounds(186, 9, 46, 20);
			}
			{
				lblCell = new JLabel();
				this.add(lblCell);
				lblCell.setText("Cell ID:");
				lblCell.setBounds(146, 12, 35, 14);
			}
			{
				btnCompute = new JButton();
				this.add(btnCompute);
				btnCompute.setText("Compute");
				btnCompute.setBounds(238, 8, 80, 23);
				btnCompute.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e){
						try{
							int val = Integer.parseInt(txtId.getText());
							int x = val / rowSize;
							int y = val % rowSize;
							lblX.setText("X: " + x);
							lblY.setText("Y: " + y);
							repaint();
						}catch (Exception e1) {
							System.out.println("Curses");
						}
					}
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void updateProbJoint(float[][] probabilities) {
		for (int i=0; i<probabilities.length; i++){
			for (int j=0; j<=i; j++){
				jTable1.getModel().setValueAt(probabilities[i][j]+"", i, j+1);
			}
		}
		//((DefaultTableCellRenderer)jTable1.getCellRenderer(xMax, yMax)).setBackground(Color.BLUE);
		repaint();
	}
}


